package com.kuang.org.service;

import com.kuang.bascic.Myexception.BusinessException;
import com.kuang.bascic.service.BaseServiceImpl;
import com.kuang.bascic.utils.AjaxResult;
import com.kuang.bascic.utils.BaiduAuditUtils;
import com.kuang.org.domain.Employee;
import com.kuang.org.domain.Shop;
import com.kuang.org.mapper.EmployeeMapper;
import com.kuang.org.mapper.ShopMapper;
import org.csource.common.MyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements ShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public AjaxResult settlement(Shop shop) {
        if (shop.getName().isEmpty()){
            throw new BusinessException("店铺名为空");
        }
        if (!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new BusinessException("店铺名不合法");
        }
        // 保存到employee 返回新增的主键
        Employee admin = shop.getAdmin();
        employeeMapper.addEmployee(admin);
        // 保存到shop表里有返回的主键就是admin_id   返回主键id  shop的主键id
        shop.setAdmin_id(admin.getId());
        shopMapper.addDepartment(shop);
        // 更行employee 的shop_id
        admin.setShop_id(shop.getId());
        employeeMapper.UpdateEmployee(admin);
        return AjaxResult.me();
    }
}
